<?php
/**
 * Description: 获取知识点错题记录
 * User: devforma
 * Date: 15/1/26
 * Time: 15:07
 */

class NoteErrorQuestions implements iController {
    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $uddb;
        
        if (empty($queryArgs['note_id']) || !filter_var($queryArgs['note_id'], FILTER_VALIDATE_INT) || validateUser($queryArgs) !== 1)
            return genOutput(1000);

        //获取题目数据
        $sql = "SELECT * FROM `dp_history_user_tests` WHERE `user_id`=:user AND `note_id`=:note AND `is_wrong`=1";
        $data = $uddb->getResults($sql, [':user' => $queryArgs['user_id'], ':note' => $queryArgs['note_id']]);

        $list = array();
        $answers = array();
        if (!empty($data)) {
            foreach ($data as $q) { //构造答案数组
                $answers["$q->question_id"] = [
                    'id'        => (int)$q->question_id,
                    'answer'    => $q->user_answer,
                    'is_wrong'  => $q->is_wrong ? true : false
                ];
            }

            $ids = array_keys($answers);

            $list = getQuestionsByIds($ids);
            $answers = array_values($answers);
        }

        return genOutput(1, array('questions' => $list, 'answers' => $answers));
    }
}
